import { useRequest } from "../../../utils/request";
import dayjs from "../../../utils/day";

Component({
    properties: {},
    observers: {
        'data': function (data) {
            const { lock_time, live_time } = data

            // 循环锁房列表
            const daysList = []
            // 当前时间
            const now = dayjs()
            // 循环月份数量
            const monthNums = 2
            for (let i = 0; i < monthNums; i++) {
                const month = dayjs().add(i, 'month')
                const listItem = {
                    month: month,
                    monthText: month.format('YYYY年MM月'),
                    list: []
                }
                // 月份天数
                const daysNum = month.endOf('month').date()
                // 先留出空的位置
                for (let u = 0; u < month.day(); u++) {
                    listItem.list.push({})
                }
                for (let u = 1; u <= daysNum; u++) {
                    // 当前月份的话需要判断是否超过日期，不能设置
                    if (i === 0 && now.date() > u) {
                        listItem.list.push({
                            day: u,
                            readonly: true
                        })
                    } else {
                        // 是否锁房中
                        if (lock_time.indexOf(String(
                            month
                                .date(u)
                                .hour(0)
                                .minute(0)
                                .second(0)
                                .unix()
                        )) > -1) {
                            listItem.list.push({
                                day: u,
                                lock: true
                            })
                        }
                        // 是否入住中
                        else if (live_time.some(item => {
                            return month
                                .date(u)
                                .hour(0)
                                .minute(0)
                                .second(0)
                                .isBetween(
                                    dayjs.unix(item.start_time),
                                    dayjs.unix(item.end_time),
                                    'day',
                                    '[]'
                                )
                        })) {
                            listItem.list.push({
                                day: u,
                                use: true
                            })
                        } else {
                            listItem.list.push({
                                day: u,
                            })
                        }
                    }
                }
                daysList.push(listItem)
            }
            this.setData({
                daysList
            })
        }
    },
    data: {
        data: null,
        // 0随意选 1已经锁房 2未锁
        checkStatus: 0,
    },
    methods: {
        onLoad(options) {
            this.setData({
                id: options.id
            })
            // 获取数据
            this.request = new useRequest('api/gethousestatus', {
                data: 'data',
                params: {
                    house_id: options.id
                }
            }, this)

            // 锁房待锁房
            this.lockRequest = new useRequest('api/houselock', {
                hold: true
            }, this)
            this.unlockRequest = new useRequest('api/houseunlock', {
                hold: true
            }, this)
        },
        checkItem(e) {
            console.log(e);
            const { month, day } = e.currentTarget.dataset
            const dayData = this.data.daysList[month].list[day]
            if (
                dayData.lock && this.data.checkStatus === 2 ||
                !dayData.lock && this.data.checkStatus === 1
            ) {
                wx.showToast({
                    icon: 'none',
                    title: '房源锁定与房源解锁不能同时选择'
                })
                return
            }

            this.setData({
                [`daysList[${month}].list[${day}].check`]: !dayData.check,
            })

            if (!this.getCheckList().length) {
                this.setData({
                    checkStatus: 0
                })
            } else {
                this.setData({
                    checkStatus: dayData.lock ? 1 : 2
                })
            }
        },
        getCheckList() {
            const list = this.data.daysList
                .map(month => {
                    return {
                        month: month.month,
                        list: month.list.filter(item => item.check)
                    }
                }).map(month => {
                    return month.list.map(item => {
                        return month.month
                            .date(item.day)
                            .hour(0)
                            .minute(0)
                            .second(0)
                            .unix()
                    })
                })
            return [].concat(...list)
        },
        async onPullDownRefresh() {
            await this.request.run()
            this.setData({
                checkStatus: 0
            })
            wx.stopPullDownRefresh()
        },
        buttonClick() {
            if (this.data.checkStatus && !this.data.loading) {
                if (this.data.checkStatus === 1) {
                    wx.showModal({
                        title: '提示',
                        content: '是否解锁房源？',
                        success: result => {
                            if (result.confirm) {
                                this.unlockRequest.run({
                                    house_id: this.data.id,
                                    lock_time: this.getCheckList()
                                }).then(() => {
                                    wx.startPullDownRefresh()
                                })
                            }
                        }
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '是否锁定房源？',
                        success: result => {
                            if (result.confirm) {
                                this.lockRequest.run({
                                    house_id: this.data.id,
                                    lock_time: this.getCheckList()
                                }).then(() => {
                                    wx.startPullDownRefresh()
                                })
                            }
                        }
                    })
                }
            }
        }
    }
});
